/*
 * ShopCategoryProvider.java
 * business
 *
 * Created by ChenTao on 2017/3/2.
 *
 * Copyright (c) 2017年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.ui.widget.recyclerview.provider;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yidiandao.business.R;
import com.yidiandao.business.data.event.RefreshShopCategoryStateEvent;
import com.yidiandao.business.data.response.QueryShopCategoryResponse;
import com.yidiandao.business.utils.UIUtils;

import org.greenrobot.eventbus.EventBus;

import me.drakeet.multitype.ItemViewProvider;

/**
 * 店铺详情页面分类Item-Provider
 *
 * <p>Created by ChenTao(chentao7v@gmail.com) on 2017/3/2 14:33
 */
public class ShopCategoryProvider
        extends ItemViewProvider<
        QueryShopCategoryResponse.ShopCategory, ShopCategoryProvider.ViewHolder> {

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(
            @NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View rootView = inflater.inflate(R.layout.item_category_list, parent, false);
        return new ViewHolder(rootView);
    }

    @Override
    protected void onBindViewHolder(
            @NonNull final ViewHolder holder,
            @NonNull QueryShopCategoryResponse.ShopCategory data) {
        holder.tvCategoryName.setText(data.getName());

        if (data.isSelected()) {
            holder.tvCategoryName.setTextColor(UIUtils.getColor(R.color.colorSelector));
            holder.rlytBgCategory.setBackgroundColor(UIUtils.getColor(R.color.bg_page));
        } else {
            holder.tvCategoryName.setTextColor(UIUtils.getColor(R.color.text_color_7));
            holder.rlytBgCategory.setBackgroundColor(UIUtils.getColor(R.color.white));
        }

        holder.rlytBgCategory.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        RefreshShopCategoryStateEvent event = new RefreshShopCategoryStateEvent();
                        event.setPosition(getPosition(holder));
                        EventBus.getDefault().post(event);
                    }
                });
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        private TextView tvCategoryName;

        private RelativeLayout rlytBgCategory;

        public ViewHolder(View itemView) {
            super(itemView);

            rlytBgCategory = (RelativeLayout) itemView.findViewById(R.id.rl_bg_category);
            tvCategoryName = (TextView) itemView.findViewById(R.id.tv_category_name);
        }
    }
}
